﻿using ProductSystem.BLL.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ProductSystem.Web.Controllers
{
    public class CustomHandlerErrorAttribute : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
            if (filterContext.ExceptionHandled)
            {
                return;
            }


            LogHelper.Instance().WriteLog("系统错误", filterContext.Exception);
            //记录日志信息
            filterContext.ExceptionHandled = true;

            var errorModel = new HandleErrorInfo(new Exception(string.Format("内部错误", filterContext.RequestContext.HttpContext.Request.UrlReferrer), filterContext.Exception), "Home", "OtherError");
            filterContext.Controller.ViewData.Model = errorModel;
            String urlhost = filterContext.RequestContext.HttpContext.Request.ServerVariables["HTTP_HOST"];
            var destinationUrl = String.Format("http://{0}{1}", urlhost,
                                               "/Home/OtherError");
            filterContext.Result = new RedirectResult(destinationUrl);
        }
    }
}